﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AlibabaCloud.SDK;

namespace hnrzxt.Controllers
{
    public class InfosController : Controller
    {
        private readonly ApplicationDbContext _db;
        public InfosController(ApplicationDbContext db)
        {
            _db = db;
        }
        public IActionResult Index()
        {
            return View();
        }
        public String ModifytheInfos(hr_jbxx hr_jbxx)
        {
            var a = _db.hr_jbxx.FirstOrDefault(p => p.uuid == hr_jbxx.uuid);

            _db.hr_jbxx.Remove(a);
            _db.SaveChanges();
            _db.hr_jbxx.Add(hr_jbxx);
            _db.SaveChanges();
            return "200";

        }

        public String deletetheListInfos(int id)
        {

            var a = _db.hr_jbxx.FirstOrDefault(p => p.Id == id);
            _db.hr_jbxx.Remove(a);
            _db.SaveChanges();
            return "删除完成";
        }

        public String Modifythegangwei(hr_gwxx hr_gwxx)
        {
            var a = _db.hr_gwxx.FirstOrDefault(p => p.uuid == hr_gwxx.uuid);

            if (a != null)
            {
                _db.hr_gwxx.Remove(a);
                _db.SaveChanges();
            }
   
            _db.hr_gwxx.Add(hr_gwxx);
            _db.SaveChanges();
            return "200";

        }

        public String Modifytheshengyu(hr_hyzk hr_hyzk)
        {
            var a = _db.hr_hyzk.FirstOrDefault(p => p.uuid == hr_hyzk.uuid);

            if (a != null)
            {
                _db.hr_hyzk.Remove(a);
                _db.SaveChanges();

            }

            _db.hr_hyzk.Add(hr_hyzk);
            _db.SaveChanges();
            return "200";

        }
        public String ModifytheHealth(hr_situation hr_situation)
        {

            //var a  = _db.hr_jbxx.FirstOrDefault(p => p.uuid == uuid);

            var a = _db.hr_situation.FirstOrDefault(p => p.uuid == hr_situation.uuid);

            if (a != null)
            {
                _db.hr_situation.Remove(a);
                _db.SaveChanges();

            }

            _db.hr_situation.Add(hr_situation);
            _db.SaveChanges();
            return "200";

        }



        public String putthejcinfo(hr_jcjl hr_jcjl)
        {

            var a = _db.hr_jcjl.FirstOrDefault(p => p.Id == hr_jcjl.Id);

            if (a != null)//有
            {
                _db.hr_jcjl.Remove(a);
                _db.SaveChanges();

            }
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.FromObject(hr_jcjl);


            jObject.Remove("Id");
            hr_jcjl h = jObject.ToObject<hr_jcjl>();
            //hr_diseasa = JsonConvert.DeserializeObject<hr_diseasa>(jObject.ToJson());

            _db.hr_jcjl.Add(h);
            _db.SaveChanges();
            return "200";

        }


        public String putthefamilyinfo(hr_jtzk hr_jtzk)
        {

            var a = _db.hr_jtzk.FirstOrDefault(p => p.Id == hr_jtzk.Id);

            if (a != null)//有
            {
                _db.hr_jtzk.Remove(a);
                _db.SaveChanges();

            }
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.FromObject(hr_jtzk);


            jObject.Remove("Id");
            hr_jtzk h = jObject.ToObject<hr_jtzk>();
            //hr_diseasa = JsonConvert.DeserializeObject<hr_diseasa>(jObject.ToJson());

            _db.hr_jtzk.Add(h);
            _db.SaveChanges();
            return "200";
        }




        public String puttheworkinfo(hr_gzjl hr_gzjl)
        {
            var a = _db.hr_gzjl.FirstOrDefault(p => p.Id == hr_gzjl.Id);

            if (a != null)//有
            {
                _db.hr_gzjl.Remove(a);
                _db.SaveChanges();

            }
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.FromObject(hr_gzjl);


            jObject.Remove("Id");
            hr_gzjl h = jObject.ToObject<hr_gzjl>();
            //hr_diseasa = JsonConvert.DeserializeObject<hr_diseasa>(jObject.ToJson());

            _db.hr_gzjl.Add(h);
            _db.SaveChanges();
            return "200";

        }


        public String puttheEducationsinfo(hr_jyxx hr_jyxx)
        {
            var a = _db.hr_jyxx.FirstOrDefault(p => p.Id == hr_jyxx.Id);

            if (a != null)//有
            {
                _db.hr_jyxx.Remove(a);
                _db.SaveChanges();

            }
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.FromObject(hr_jyxx);


            jObject.Remove("Id");
            hr_jyxx h = jObject.ToObject<hr_jyxx>();
            //hr_diseasa = JsonConvert.DeserializeObject<hr_diseasa>(jObject.ToJson());

            _db.hr_jyxx.Add(h);
            _db.SaveChanges();
            return "200";

        }
        //public String puttheDiesesinfo(hr_diseasa hr_diseasa)
        //{

        //    _db.hr_diseasa.Add(hr_diseasa);

        //    _db.SaveChanges();

        //    return "200";

        //}

        public String puttheDiesesInfo(hr_diseasa hr_diseasa)
        {

            //time = 2022 - 10 - 07 & diseasehistory = 99999 & diseasestatus = 9 & uuid = 3dd9f4a2 - 2e4d - 41ee - a9c3 - 68ad556aa47d//
            var a = _db.hr_diseasa.FirstOrDefault(p => p.Id == hr_diseasa.Id);

            if (a != null)//有
            {
                _db.hr_diseasa.Remove(a);
                _db.SaveChanges();

            }
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.FromObject(hr_diseasa);


            jObject.Remove("Id");
            hr_diseasa h = jObject.ToObject<hr_diseasa>();
            //hr_diseasa = JsonConvert.DeserializeObject<hr_diseasa>(jObject.ToJson());

            _db.hr_diseasa.Add(h);
            _db.SaveChanges();
            return "200";


        }
        public String PutBasicInfos(hr_jbxx hr_jbxx)
        {

            var uuid = Guid.NewGuid().ToString("D");


            hr_jbxx.uuid = uuid;
            _db.hr_jbxx.Add(hr_jbxx);
            _db.SaveChanges();


            return uuid;
        }



        public String _Putfiles(hr_cpFiless hr_cpFiless)
        {


            string[] cpshuzu = hr_cpFiless.cpFile.Split(new char[] { ',' });
            for (int i = 0; i < cpshuzu.Length; i++)
            {
                var snow = Guid.NewGuid().ToString();
                hr_cpFiless.Id = snow;
                hr_cpFiless.cpFile = cpshuzu[i];
                _db.hr_cpFiless.Add(hr_cpFiless);

                _db.SaveChanges();
            }
            return "200";
        }

        public String _Putthehealthfiles(hr_cpFilejiaoyu hr_cpFilejiaoyu)
        {


            string[] cpshuzu = hr_cpFilejiaoyu.cpFile.Split(new char[] { ',' });
            for (int i = 0; i < cpshuzu.Length; i++)
            {
                var snow = Guid.NewGuid().ToString();
                hr_cpFilejiaoyu.Id = snow;
                hr_cpFilejiaoyu.cpFile = cpshuzu[i];
                _db.hr_cpFilejiaoyu.Add(hr_cpFilejiaoyu);

                _db.SaveChanges();
            }
            return "200";
        }
        //public String Putfiles(string s,string uuid)
        //{

        //    string[] cpshuzu = s.Split(new char[] { ',' });


        //    for (int i = 0; i < cpshuzu.Length; i++)
        //    {



        //        hr_cpFiless h =  new hr_cpFiless();
        //        h.uuid= uuid;
        //        h.cpFile = cpshuzu[i];
        //        _db.hr_cpFiless.Add(h);

        //        _db.SaveChanges();


        //    }

        //    return "200";

        //}
        public String PutGangweiInfos(hr_gwxx hr_gwxx)
        {


            _db.hr_gwxx.Add(hr_gwxx);

            _db.SaveChanges();


            return "200";

        }
        public String deleteDisease(int _id)
        {


            var _o = _db.hr_diseasa.FirstOrDefault(p => p.Id == _id);
            if (_o != null)
            {

                _db.hr_diseasa.Remove(_o);
                _db.SaveChanges();


            }

            return "200";

        }

        public Boolean checktheidnumber(String id)
        {


            var _id = _db.hr_jbxx.FirstOrDefault(p => p.idNumber == id);
            if (_id != null)//exist the id;
            {

                return true;

            }
            else
            {
                return false;
            }
            

        }

        public String deletethefiles(string _id)
        {
            var _o = _db.hr_cpFiless.FirstOrDefault(p => p.Id == _id);


            if (_o != null)
            {
                _db.hr_cpFiless.Remove(_o);
                _db.SaveChanges();



            }
            return "200";




        }
        public String deletethejiaoyuxinxi(string _id)
        {
            var _o = _db.hr_cpFilejiaoyu.FirstOrDefault(p => p.Id == _id);


            if (_o != null)
            {
                _db.hr_cpFilejiaoyu.Remove(_o);
                _db.SaveChanges();



            }
            return "200";




        }
        public String deletetherecords(int _id)
        {
            var _o = _db.hr_jcjl.FirstOrDefault(p => p.Id == _id);


            if (_o != null)
            {
                _db.hr_jcjl.Remove(_o);
                _db.SaveChanges();



            }
            return "200";




        }
        public String deletefamilys(int _id)
        {


            var _o = _db.hr_jtzk.FirstOrDefault(p => p.Id == _id);


            if (_o != null)
            {
                _db.hr_jtzk.Remove(_o);
                _db.SaveChanges();



            }
            return "200";




        }
        public String deletetheworkins(int _id)
        {

            var _o = _db.hr_gzjl.FirstOrDefault(p => p.Id == _id);


            if (_o != null)
            {
                _db.hr_gzjl.Remove(_o);
                _db.SaveChanges();



            }
            return "200";

        }

        public String deleteEducation(int _id)
        {
            var _o = _db.hr_jyxx.FirstOrDefault(p => p.Id == _id);


            if (_o!= null)
            {
                _db.hr_jyxx.Remove(_o);
                _db.SaveChanges();



            }
            return "200";


        }
        


        public String PutHunyuInfos(hr_hyzk hr_hyzk)
        {

            _db.hr_hyzk.Add(hr_hyzk);
            _db.SaveChanges();

            return "200";
        }


        public String PuttheSituation(hr_situation hr_situation)
        {

            _db.hr_situation.Add(hr_situation);
            _db.SaveChanges();

            return "";
        }


        public String Createtheuuid()
        {






            return  Guid.NewGuid().ToString("D"); 


        }




        public String PutHmcBasic ()
        {



            //if (o instanceof BasicInfomations){



            //}
            //_db.BasicInfomations.Add(basicInfomations);
            //_db.SaveChanges();




            return "";
        }



        public List<Dictionary<string, object>> Getjibinginfos(string Jbxxid)
        {


            List<hr_diseasa> rccplist = _db.hr_diseasa.Where(P => P.uuid == Jbxxid).ToList();

            List<Dictionary<string, object>> list = new();
            for (int i = 0; i < rccplist.Count; i++)
            {
                Dictionary<string, object> map = new();
                map.Add("time", rccplist[i].time);
                map.Add("diseasehistory", rccplist[i].diseasehistory);
                map.Add("diseasestatus", rccplist[i].diseasestatus);


                list.Add(map);
            }
            return list;


        }


        public Dictionary<string, object> GetfamilysinfosbyList(string Jbxxid)
        {


            List<hr_jtzk> li = _db.hr_jtzk.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }
        public Dictionary<string, object> GettheCorn(string Jbxxid)
        {


            //List<hr_jbxx> jbxxlist = _db.hr_jbxx.Where(p => p.uuid == Jbxxid).ToList();
            //var corn = jbxxlist[0];


            Dictionary<string, object> map = new();
            var corn = _db.hr_jbxx.Where(p => p.uuid == Jbxxid);
            var gangweiInfos = _db.hr_gwxx.Where(p => p.uuid == Jbxxid);
            var hunyinzhuangkuang = _db.hr_hyzk.Where(p => p.uuid == Jbxxid);
            var jiankangzhuangkuang = _db.hr_diseasa.Where(p => p.uuid == Jbxxid);
            var jiaoyuxinxi  = _db.hr_jyxx.Where(p => p.uuid == Jbxxid);
            var gongzuojingli = _db.hr_gzjl.Where(p => p.uuid == Jbxxid);
            var jiatingzhuangkuang = _db.hr_jtzk.Where(p => p.uuid == Jbxxid);
            var jiangchengrecords = _db.hr_jcjl.Where(p => p.uuid == Jbxxid);

            var situations = _db.hr_situation.Where(p => p.uuid == Jbxxid);

            map.Add("corn",corn);
            map.Add("gangwei", gangweiInfos);
            map.Add("hunyin", hunyinzhuangkuang);
            map.Add("jiankangzhuangkuang", jiankangzhuangkuang);
            map.Add("jiaoyuxinxi", jiaoyuxinxi);
            map.Add("gongzuojingli", gongzuojingli);
            map.Add("jiatingzhuangkuang", jiatingzhuangkuang);
            map.Add("jiangchengrecords", jiangchengrecords);
            map.Add("situ", situations);
 
            return map;


        }


        public Dictionary<string, object> GetJobinfosbyList(string Jbxxid)
        {


            List<hr_gzjl> li = _db.hr_gzjl.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }
        public Dictionary<string, object> GettheSituationsInfo(string Jbxxid)
        {


            List<hr_cpFiless> li = _db.hr_cpFiless.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
           

            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }

        public Dictionary<string, object> Getthejiaoyuininfos(string Jbxxid)
        {


            List<hr_cpFilejiaoyu> li = _db.hr_cpFilejiaoyu.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();


            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }


        public Dictionary<string, object> GetjibinginfosbyList(string Jbxxid)
        {


            List<hr_diseasa> li = _db.hr_diseasa.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }


        public Dictionary<string, object> GeteducationsInfosbyList(string Jbxxid)
        {


            List<hr_jyxx> li = _db.hr_jyxx.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }

        public Dictionary<string, object> GetejiangchengInfosbyList(string Jbxxid)
        {


            List<hr_jcjl> li = _db.hr_jcjl.Where(P => P.uuid == Jbxxid).OrderByDescending(x=> x.type).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", li);


            return map;


        }


        //public FileResult download()
        //{
        //    string filePath = HttpContext.Server.MapPath("~/Document/123.txt");//路径
        //    HttpContext.Current.Server.MapPath(commonPath);
        //    return File(filePath, "text/plain", "1234.txt"); //1234.txt是客户端保存的名字
        //}





    }
}
